package com.haiwen.boot.service.lmpl;

import com.haiwen.boot.mapper.commentMapper;
import com.haiwen.boot.mapper.commentReplyMapper;
import com.haiwen.boot.pojo.comment;
import com.haiwen.boot.pojo.commentReply;
import com.haiwen.boot.service.CommentService;
import com.haiwen.boot.utils.RedisContext;
import com.haiwen.boot.utils.RedisUtil;
import com.haiwen.boot.vo.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private commentMapper commentMapper;
    @Autowired
    private commentReplyMapper commentReplyMapper;
    @Autowired
    private ResultMap resultMap;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public ResultMap comment(Integer blogID, Integer userID, String content) {
        try {
            Date date = new Date();
            String replace = content.replace(" ", "");
            if (replace.equals("")){
                    return  resultMap.code(500).message("未知错误").success();
            }
            comment comment = new comment(blogID, userID, date, date, content);
            commentMapper.insertSelective(comment);
            this.FrequentComments(blogID,userID,date);
            return resultMap.code(200).message("评论成功").success();
        } catch (Exception ignored) {
            return resultMap.code(500).message("出错了").success();
        }
    }
    @Override
    public ResultMap  ReplyComment(Integer replyUserID, Integer blogID, Integer userID, Integer commentID, String content) {
        try {
            String isUnique = commentMapper.QueryUniqueComments(blogID, commentID);
            String replace = content.replace(" ", "");
            if (isUnique==null||replace.equals("")){
                return  resultMap.code(500).message("未知错误").success();
            }
            if (replyUserID.equals(userID)){
                return   resultMap.code(500).message("不能自己回复自己哦").success();
            }
            Date date = new Date();
            commentReply commentReply = new commentReply(replyUserID, blogID, commentID, userID, date, content);
            commentReplyMapper.insertSelective(commentReply);
            this.FrequentComments(blogID,userID,date);
            return resultMap.code(200).message("回复成功").success();
        } catch (Exception e) {
            e.printStackTrace();
            return resultMap.code(500).message("出错了").success();
        }
    }

    @Override
    public ResultMap ShowComments(Integer blogID) {
        try {
            List<Object> commentsCache=redisUtil.lGet(RedisContext.PARENT_COMMENTS_KEY+blogID, 0, -1);
            if (commentsCache.isEmpty()){
                List<comment> comments = commentMapper.selectByBlogID(blogID);
                if (comments.isEmpty()){
                    return  resultMap.code(500).message("暂无评论").success();
                }
                for (comment comment:comments) {
                    List<commentReply> commentReplies = commentReplyMapper.ReplyCommentsShow(blogID, comment.getCommentId());
                    comment.setCommentReplyList(commentReplies);
                }
                redisUtil.lSet(RedisContext.PARENT_COMMENTS_KEY+blogID,comments,60*2);
                commentsCache= redisUtil.lGet(RedisContext.PARENT_COMMENTS_KEY+blogID, 0, -1);
            }
            return resultMap.code(200).message("成功").success().content(commentsCache);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 统一设置频繁访问
     * @param blogID 博客id
     * @param userID 用户id
     */
    public void FrequentComments(Integer blogID, Integer userID,Date date){
        redisUtil.remove(RedisContext.PARENT_COMMENTS_KEY+blogID);
        redisUtil.hdel(RedisContext.BLOGID_KEY+blogID,RedisContext.USERID_KEY+userID);
        Map<String, Object> userIdKey=new HashMap<>();
        userIdKey.put(RedisContext.USERID_KEY+userID,date);
        redisUtil.hmset(RedisContext.BLOGID_KEY+blogID,userIdKey);
    }
}
